<template>
  <div class="yy-queue-priority_config">
    <label>规则</label>
    <div style="padding: 15px;background: #f1f1f1;">
      <RuleCondition ref="ruleConditionRef" :domain-data="domainData"  :condition-data="ruleForm.trigger"/>
    </div>
  </div>
</template>
<script setup lang="ts">
import {defineEmits, defineExpose, defineProps, nextTick, onBeforeMount, onBeforeUnmount, onMounted, reactive, ref} from "vue";
import {FuncClass, RuleCondition} from "logic-component-lib";

  const props = defineProps({
    field: {
      type: Object,
      required: false,
      default: null
    }
  })

  const domainData = ref<{domain: FuncClass[],constant: FuncClass[], func: FuncClass[]}>({
    domain: [
      {
        value: 'patient',
        label: '患者',
        type: 'domain',
        fields: [
          {
            label: '年龄',
            value: 'age'
          }, {
            label: '性别',
            value: 'sex',
          }, {
            label: '预约标识',
            value: 'scheduleFlag',
          },{
            label: '回诊标识',
            value: 'reviewFlag',
          },{
            label: '复诊标识',
            value: 'followFlag',
          },{
            label: '绿通标识',
            value: 'priorityFlag'
          }
        ]
      }
    ],
    constant: [
      {
        type: 'constant',
        value: 'string',
        control: 'text',
        label: '字符'
      },
      {
        type: 'constant',
        value: 'number',
        control: 'number',
        label: '数值'
      },
      {
        value: 'bool',
        label: '布尔值',
        type: 'domain',
        fields: [
          {
            label: '是',
            value: '1'
          }, {
            label: '否',
            value: '0'
          }
        ]
      },
      {
        value: 'priorityFlag',
        label: '绿通标识',
        type: 'domain',
        fields: [
          {
            label: '残疾人',
            value: '1'
          }, {
            label: '孕妇',
            value: '2',
          }, {
            label: '军人',
            value: '3'
          }, {
            label: '烈士家属',
            value: '4'
          }
        ]
      },
      {
        value: 'sex',
        label: '性别',
        type: 'domain',
        fields: [
          {
            label: '男',
            value: '1'
          }, {
            label: '女',
            value: '2',
          }, {
            label: '未知',
            value: '3'
          }
        ]
      }
    ],
    func: [
      {
        type: 'logic',
        value: 'and',
        label: '且'
      }, {
        type: 'logic',
        value: 'or',
        label: '或'
      },{
        type: 'operation',
        value: '==',
        label: '等于'
      }, {
        type: 'operation',
        value: '!=',
        label: '不等于'
      }, {
        type: 'operation',
        value: '>=',
        label: '大于等于'
      }, {
        type: 'operation',
        value: '<=',
        label: '小于等于'
      }
    ]
  })
  const ruleForm = reactive({
    trigger: null
  })

  const ruleConditionRef = ref()

  const getFormFieldValue = () => {
    const ruleConditions = ruleConditionRef.value.getRuleConditions()
    return {
      config: JSON.stringify(ruleConditions)
    }
  }

  const initFormData = (data) => {
    ruleForm.trigger = data ? JSON.parse(data.config) : null
  }

  const resetFields = () => {
    ruleForm.trigger = null
  }

  defineExpose({
    getFormFieldValue,
    initFormData,
    resetFields
  })

</script>
<style scoped lang="scss">
  .yy-queue-priority_config {
    display: flex;
    flex-direction: row;
    & > label {
      flex: 0 0 54px;
      display: inline-flex;
      justify-content: flex-end;
      align-items: center;
      padding:  0 12px 0 0;
    }
  }
  ::v-deep(.qc-rule-condition_item) {
    align-items: center;
  }
</style>